﻿using System.Net;
using common;
using Letter.Net.Tcp;
using tcp_connection;

namespace tcp_client_test1;

class Program
{
    private static DateTime startTime;

    static List<ClientTcpConnection> connections = new List<ClientTcpConnection>();

    static async Task Main(string[] args)
    {
        for (int i = 0; i < 5000; i++)
        {
            var clientTcpConnection = new ClientTcpConnection();
            var client = TcpProvider.Client();
            client.ConfigureChannelOptions(new TcpClientTransportOptions() { PooledSnder = false });
            client.ConfigureFeatureOptions(new TcpClientFeatureOptions());
            client.ConfigureConnection(() =>
            {
                return clientTcpConnection;
            });
            client.Build();
            await client.ConnectAsync(IPEndPoint.Parse("127.0.0.1:20001"));
            connections.Add(clientTcpConnection);
        }

        for (int i = 0; i < connections.Count; i++)
        {
            var connection = connections[i];
            connection.Write(Message.bytes);
            await connection.FlushAsync();
        }

        startTime = DateTime.Now;

        while (true)
        {
            string? str = Console.ReadLine();
            if (str == "c")
            {
                return;
            }

            if (str == "t")
            {
                var totalSeconds = (DateTime.Now - startTime).TotalSeconds;
                long count = 0;
                foreach (var filter in connections)
                {
                    count += filter.count;
                }

                Console.WriteLine("平均每秒：" + count / totalSeconds);
            }
        }

        Console.ReadLine();
    }
}